package com.zfjt.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.zfjt.constants.InterfaceDefinition;
import com.zfjt.entity.ClientmessageBean;
import com.zfjt.entity.ReturnBean;
import com.zfjt.packnum.PackNum00035;
import com.zfjt.service.ClientmessageBeanService;
import com.zfjt.utils.StringUtil;
import com.zfjt.utils.WQUtil;

/**
 * 修改照片接口
 * @author xk
 * @version 1.0
 * @date 2017年4月19日
 */
@Controller
public class RevampImgController {
	
	private ReturnBean mReturn;
	
	private WQUtil wqUtil;
	
	@Autowired
	private ClientmessageBeanService mClientMessage;
	
	private Map<String, String> fhdata; 
	
	@RequestMapping(value="00035")
	@ResponseBody
	public ReturnBean AddImg12(HttpSession hseesion,HttpServletRequest request){
		mReturn = new ReturnBean();
		fhdata = new HashMap<>();
		wqUtil = new WQUtil();
		//传过来的参数
		String data =  (String) hseesion.getAttribute("data");
		PackNum00035 num00035 = JSON.parseObject(data,PackNum00035.class); 
		ClientmessageBean mdata = mClientMessage.selectByPrimaryKey((short) Integer.parseInt(num00035.getId()));
		if (data.equals(InterfaceDefinition.IStatusCode.TOKEN_FAILURE)) {//校验码错误
			mReturn.setCode("001");
			mReturn.setPortnum("00035");
			fhdata.put("error", "TOKEN_ERROR");
			mReturn.setData(JSON.toJSONString(fhdata));
		}else{
			String str[] = StringUtil.convertStrToArray(mdata.getRejectImg());
			Map<String, Object> map = JSON.parseObject(num00035.getData());
			//获取Tomcat下项目根目录
			String path = request.getSession().getServletContext().getRealPath("static\\images" + "/") +"\\" + mdata.getName();
			File file = new File(path);
			if (!file.exists()) {
				file.mkdirs();
			}
			for (int i = 0; i < str.length; i++) {
				wqUtil.GenerateImage((String) map.get(str[i]),path + "\\" + str[i] + ".jpg");
			}
			mdata.setType("2");
			mClientMessage.updateByPrimaryKeySelective(mdata);
			mReturn.setCode("000");
			mReturn.setData("");
			mReturn.setPortnum("00035");
		}
		return mReturn;
	}
}
